package com.pctx.bibatracker.service.impl;

import com.pctx.bibacommon.result.Result;
import com.pctx.bibatracker.dao.HistoryPositionMapper;
import com.pctx.bibatracker.pojo.HistoryPosition;
import com.pctx.bibatracker.service.HistoryPositionService;
import com.pctx.bibatracker.util.CoordinateTransforToGaoDe;
import com.pctx.bibatracker.util.CoordinateTransform;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service("/historyPositionService")
public class HistoryPositionServiceImpl implements HistoryPositionService {
    @Autowired
    private HistoryPositionMapper historyPositionMapper;

    /**
     * 百度地图轨迹
     *
     * @param deviceNo
     * @param startTime
     * @param stopTime
     * @return
     * @throws Exception
     */
    @Override
    public Result getTrail(String deviceNo, String startTime, String stopTime) throws Exception {
        if (StringUtils.isEmpty(deviceNo)) {
            return Result.error("参数不能为空");
        }
        List<HistoryPosition> lists = historyPositionMapper.selectByDeviceUuid(deviceNo, startTime, stopTime);
        ArrayList list = new ArrayList();
        for (HistoryPosition historyPositionTwo : lists) {
            String cordone = CoordinateTransform.getCoordinate(historyPositionTwo.getCordX(), historyPositionTwo.getCordY());
            list.add(cordone);
            /*JSONObject jsonObjectone = JSONObject.fromObject(cordone);
            JSONArray jsonArrays = jsonObjectone.getJSONArray("result");
            System.out.println(jsonArrays);
            Object[] os = jsonArrays.toArray();
            for (int i = 0; i < os.length; i++) {
                JSONObject jsonObj = JSONObject.fromObject(os[i]);
                list.add(jsonObj.toString());
            }*/
        }
        return Result.success(list, "历史轨迹点获取成功");
    }

    /**
     * 百度地图前一天轨迹
     *
     * @param deviceNo
     * @return
     * @throws Exception
     */
    @Override
    public Result getTodayTrail(String deviceNo) throws Exception {
        if (StringUtils.isEmpty(deviceNo)) {
            return Result.error("参数不能为空");
        }
        Date dNow = new Date();   //当前时间
        Date dBefore = new Date();
        Calendar calendar = Calendar.getInstance(); //得到日历
        calendar.setTime(dNow);//把当前时间赋给日历
        calendar.add(Calendar.DAY_OF_MONTH, -1);  //设置为前一天
        dBefore = calendar.getTime();   //得到前一天的时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置时间格式
        String startTime = sdf.format(dBefore);    //格式化前一天
        String stopTime = sdf.format(dNow); //格式化当前时间
        List<HistoryPosition> lists = historyPositionMapper.selectByDeviceUuid(deviceNo, startTime, stopTime);
        ArrayList list = new ArrayList();
        for (HistoryPosition historyPositionTwo : lists) {
            String cordone = CoordinateTransform.getCoordinate(historyPositionTwo.getCordX(), historyPositionTwo.getCordY());
            System.out.println(cordone);
            list.add(cordone);
            /*JSONObject jsonObjectone = JSONObject.fromObject(cordone);
            JSONArray jsonArrays = jsonObjectone.getJSONArray("result");
            Object[] os = jsonArrays.toArray();
            for (int i = 0; i < os.length; i++) {
                JSONObject jsonObj = JSONObject.fromObject(os[i]);
                list.add(jsonObj.toString());
            }*/
        }
        if(list.size()==0){
            return Result.error("无该设备当前轨迹");
        }else{
            return Result.success(list, "历史轨迹点获取成功");

        }
    }

    /**
     * 高德地图轨迹
     *
     * @param deviceNo
     * @param startTime
     * @param stopTime
     * @return
     * @throws Exception
     */
    @Override
    public Result getGaoDeTrail(String deviceNo, String startTime, String stopTime) throws Exception {
        if (StringUtils.isEmpty(deviceNo)) {
            return Result.error("参数不能为空");
        }
        List<HistoryPosition> lists = historyPositionMapper.selectByDeviceUuid(deviceNo, startTime, stopTime);
        ArrayList list = new ArrayList();
        for (int i = 0; i < lists.size(); i++) {
            String cordone = CoordinateTransforToGaoDe.getCoordinate(lists.get(i).getCordX(), lists.get(i).getCordY());
            JSONObject jsonObject = JSONObject.fromObject(cordone);
            Object result = jsonObject.get("locations");
            list.add(result);
        }
        return Result.success(list, "历史轨迹点获取成功");
    }

    /**
     * 高德前一天轨迹
     *
     * @param deviceNo
     * @return
     * @throws Exception
     */
    @Override
    public Result getGaoDeTodayTrail(String deviceNo) throws Exception {
        if (StringUtils.isEmpty(deviceNo)) {
            return Result.error("参数不能为空");
        }
        Date dNow = new Date();   //当前时间
        Date dBefore = new Date();
        Calendar calendar = Calendar.getInstance(); //得到日历
        calendar.setTime(dNow);//把当前时间赋给日历
        calendar.add(Calendar.DAY_OF_MONTH, -1);  //设置为前一天
        dBefore = calendar.getTime();   //得到前一天的时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置时间格式
        String startTime = sdf.format(dBefore);    //格式化前一天
        String stopTime = sdf.format(dNow); //格式化当前时间
        List<HistoryPosition> lists = historyPositionMapper.selectByDeviceUuid(deviceNo, startTime, stopTime);
        ArrayList list = new ArrayList();
        for (HistoryPosition historyPositionTwo : lists) {
            String cordone = CoordinateTransforToGaoDe.getCoordinate(historyPositionTwo.getCordX(), historyPositionTwo.getCordY());
            JSONObject jsonObject = JSONObject.fromObject(cordone);
            Object result = jsonObject.get("locations");
            list.add(result);
        }
        if(list.size()==0){
            return Result.error("无该设备历史轨迹");
        }
        return Result.success(list, "今天历史轨迹点获取成功");
    }
}
